/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */

package cn.knet.sp.ncrm.modules.call.reportin.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.utils.DateUtils;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.call.reportin.entity.CallReportIn;
import cn.knet.sp.ncrm.modules.call.reportin.entity.CallReportInQuery;
import cn.knet.sp.ncrm.modules.call.reportin.service.CallReportInService;
import cn.knet.sp.ncrm.modules.sys.user.entity.User;
import cn.knet.sp.ncrm.common.service.ExportSynService;
import cn.knet.sp.ncrm.modules.sys.utils.ExcelUtils;
import cn.knet.sp.ncrm.modules.sys.utils.UserUtils;

/**
 * 单表生成Controller
 *
 * @author zhangbin
 * @version 2015年7月2日18:21:24
 */
@Controller
@RequestMapping(value = "${adminPath}/call/reoprt/callReportIn")
public class CallReportInController extends BaseController {

	@Autowired
	private CallReportInService callReportInService;

	@Autowired
	private ExportSynService exportSynService;

	@ModelAttribute
	public CallReportIn get(@RequestParam(required = false) String id) {
		CallReportIn entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = callReportInService.get(id);
		}
		if (entity == null) {
			entity = new CallReportIn();
		}
		return entity;
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}

	private void init(CallReportInQuery in) {
		Date now = DateUtils.parseDate(DateUtils.formatDate(new Date(), "yyyy-MM-dd"));
		if (in.getBeginDate() == null) {
			in.setBeginDate(now);
		}
		if (in.getEndDate() == null) {
			in.setEndDate(now);
		}
		if (StringUtils.isNotBlank(in.getCallingId())) {

			in.setCallId("000010100000" + in.getCallingId().trim());
		}
		if (StringUtils.isBlank(in.getStatType())) {
			in.setStatType("1"); //查询班次的统计
		}
		User currentUser = UserUtils.getUser();
		in.setCurrentUserId(currentUser.getId()); //设置当前用户ID
		in.setCurrentGroupId(currentUser.getOffice().getId()); //设置组别
		if ("2".equals(UserUtils.getUser().getUserType())) {
			in.setAllUser(true);
			in.setAllGroup(false);
		} else if ("1".equals(UserUtils.getUser().getUserType())) {
			in.setAllUser(true);
			in.setAllGroup(true);
		}

	}

	@RequiresPermissions("call:reoprt:callReportIn:view")
	@RequestMapping(value = {"/list", ""})
	public String list(CallReportInQuery callReportInQuery, HttpServletRequest request, HttpServletResponse response,
					   Model model) {
		init(callReportInQuery);
		Page<CallReportIn> query = new Page<CallReportIn>(request, response, 100);
		query.setOrderBy("a.OFFICE_ID,CALLING_ID desc");
		Page<CallReportIn> page = callReportInService.findStatPage(query, callReportInQuery);
		model.addAttribute("page", page);
		return "modules/call/reoprtin/callReportInList";
	}

	/**
	 * 导出统计报表
	 * @author zhangbin
	 * @date 2015 /07/07 09:17:48
	 * @param callReportInQuery 查询参数
	 * @return 重定向到文档下周
	 */
	@RequiresPermissions("call:reoprt:callReportIn:view")
	@RequestMapping(value = {"/export"})
	public String export(CallReportInQuery callReportInQuery, HttpServletRequest request, HttpServletResponse response,
						 Model model) {
		init(callReportInQuery);
		Page<CallReportIn> query = new Page<CallReportIn>();
		query.setOrderBy("a.OFFICE_ID,CALLING_ID desc");
		Page<CallReportIn> result = callReportInService.findStatPage(query, callReportInQuery);
		ExcelUtils.Sheet sheet = new ExcelUtils.Sheet();
		sheet.setName("呼入统计");
		sheet.setList(result.getList());
		sheet.setRows(true);
		sheet.addColumn("工号", "shortId")
				.addColumn("姓名", "userName")
				.addColumn("部门", "office.name")
				.addColumn("统计开始日期", "createDate")
				.addColumn("统计结束日期", "statDate")
				.addColumn("请求呼入量", "callCount")
				.addColumn("呼入接起量", "successCallCount")
				.addColumn("呼入接起率", "successCallRate","percent")
				.addColumn("总通话时长", "callTime","seconds-time","hh:mm:ss")
				.addColumn("平均通话时长", "callTimeAvg","seconds-time","hh:mm:ss")
				.addColumn("应答时长", "successCallTime","seconds-time","hh:mm:ss")
				.addColumn("平均应答时长", "successCallTimeAvg","seconds-time","hh:mm:ss")
				.addColumn("振铃时长", "ringTime","seconds-time","hh:mm:ss")
				.addColumn("振铃次数", "ringCount")
				.addColumn("平均振铃时长", "ringAvgTime","seconds-time","hh:mm:ss")
				.addColumn("最长通话", "callMaxTime","seconds-time","hh:mm:ss")
				.addColumn("示忙时长", "busyTime","seconds-time","hh:mm:ss")
				.addColumn("示闲时长", "freeTime","seconds-time","hh:mm:ss")
				.addColumn("长通话个数", "callLongCount")
				.addColumn("坐席挂机", "sCalled")
				.addColumn("客户挂机", "cCalled");
		return exportSynService.exportSyn(sheet,model);
	}

}